<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns table-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName==='1'">收起筛选</p>
              <p v-if="activeName===''">展开筛选</p>
            </div>
          </template>
          <div class="select-box-content">
            <div class="select-box-content-name">故障类型：</div>
            <div
              class="select-box-content-all"
              @click="cancelSelect('hitchTypeCode')"
            >全部</div>
            <div
              v-for="(item, index) in hitchTypeArr"
              :key="index"
              class="select-box-content-list"
              :style="{ color: hitchTypeCode === item.hitchTypeCode ? '#54DEE2' : '#C1CFD2' ,fontWeight : hitchTypeCode === item.hitchTypeCode ? '600':'400' }"
              @click="selectHitchTypeCode(item.hitchTypeCode)"
            >{{ item.hitchTypeName }}</div>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">设备名称：</div>
            <div
              class="select-box-content-all"
              @click="cancelSelect('equipmentCode')"
            >全部</div>
            <div
              v-for="(item, index) in equipmentArr"
              :key="index"
              class="select-box-content-list"
              :style="{ color: equipmentCode === item.equipmentCode ? '#54DEE2' : '#C1CFD2' ,fontWeight : equipmentCode === item.equipmentCode ? '600':'400' }"
              @click="selectEquipmentCode(item.equipmentCode)"
            >{{ item.equipmentName }}</div>
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">维修日期：</div>
            <el-date-picker
              v-model="repairDate"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              clearable
              @change="queryPageList()"
            />
          </div>
          <div class="select-box-content">
            <div class="select-box-content-name">执行人：</div>
            <div
              class="select-box-content-all"
              @click="cancelSelect('repairUserId')"
            >全部</div>
            <div
              v-for="(item, index) in userList"
              :key="index"
              class="select-box-content-list"
              :style="{ color: repairUserId === item.id ? '#54DEE2' : '#C1CFD2' ,fontWeight : repairUserId === item.id ? '600':'400' }"
              @click="selectRepairUserId(item.id)"
            >{{ item.username }}</div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:typenodeslot>
      <SelectButton
        :topitem-select-list="topitemSelectList"
        @topitemtype="topitemtype"
      />
    </template>
    <template v-slot:content>
      <el-table
        ref="tableData"
        v-loading="loading"
        highlight-current-row
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        :row-key="
          row => {
            return row.repairTaskCode;
          }
        "
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" :reserve-selection="true" />
        <el-table-column type="index" label="序号" width="50">
          <template
            slot-scope="scoped"
          >{{ scoped.row.repairTaskCode?scoped.$index+1:'' }}</template>
        </el-table-column>
        <el-table-column prop="hitchTypeName" label="故障类型名称" />
        <el-table-column prop="equipmentName" label="设备名称" />
        <el-table-column prop="hitchKeywordNames" label="故障关键字">
          <template slot-scope="scoped">
            <div class="table-btns-box">
              <div
                v-if="scoped.row.repairTaskCode"
                class="table-btns detail-btns"
                @click="queryKey(scoped.row)"
              >查看关键字</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="pictures" label="故障图片">
          <template slot-scope="scoped">
            <div class="table-btns-box">
              <div
                v-if="scoped.row.repairTaskCode"
                class="table-btns detail-btns"
                @click="queryPicture(scoped.row)"
              >查看图片</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="videos" label="故障视频">
          <template slot-scope="scoped">
            <div class="table-btns-box">
              <div
                v-if="scoped.row.repairTaskCode"
                class="table-btns detail-btns"
                @click="queryVideo(scoped.row)"
              >查看视频</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="result" label="维修结果">
          <template
            v-if="scoped.row.repairRecordVO"
            slot-scope="scoped"
          >{{ scoped.row.repairRecordVO?scoped.row.repairRecordVO.result:'' }}</template>
        </el-table-column>
        <el-table-column type="expand" label="损耗备件" width="150">
          <template v-if="scoped.row.repairRecordVO" slot-scope="scoped">
            <el-table
              v-loading="loading"
              highlight-current-row
              :data="scoped.row.repairRecordVO.repairConsumeVoList"
              stripe
              border
              height="100%"
              style="position: relative"
            >
              <el-table-column type="index" label="序号" width="50" />
              <el-table-column prop="deviceName" label="备件名称" />
              <el-table-column prop="num" label="数量" />
            </el-table>
          </template>
        </el-table-column>
        <el-table-column prop="handUsername" label="维修人" />
        <!-- <template v-if="scoped.row.repairRecordVO" slot-scope="scoped">
            {{ getName(scoped.row.repairRecordVO.repairUserId) }}
          </template>
        </el-table-column>-->
        <el-table-column label="结果描述">
          <template slot-scope="scoped">
            <div class="table-btns-box">
              <div
                v-if="scoped.row.repairTaskCode"
                class="table-btns detail-btns"
                @click="queryDetail(scoped.row)"
              >点击查看</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="repairDate" label="维修日期">
          <template
            v-if="scoped.row.repairRecordVO"
            slot-scope="scoped"
          >{{ scoped.row.repairRecordVO?scoped.row.repairRecordVO.repairDate:'' }}</template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template v-if="scoped.row.equipmentCode" slot-scope="scoped">
            <div class="table-btns-box">
              <div class="table-btns edit-btns" @click="edit(scoped.row)">
                <i class="el-icon-edit-outline"></i>
                <p>修改</p>
              </div>
              <div class="table-btns del-btns" @click="delOne(scoped.row)">
                <i class="el-icon-delete"></i>
                <p>删除</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
      <AddEditDialog
        ref="addEditDialog"
        :topitemtype-id="topitemtypeId"
        :hitch-type-arr="hitchTypeArr"
        :user-list="userList"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Video
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showVideoDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Picture
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showPictureDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Key
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showKeyDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import repairRecordApi from "@/api/equip-repair-manage/repair-record";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import SelectButton from "@/components/equipTypeSelectButton/selcetButton.vue";
import Video from "@/components/detailDialog/video-dialog.vue";
import Picture from "@/components/detailDialog/picture-dialog.vue";
import Key from "@/components/detailDialog/key-dialog.vue";
import repairTypeApi from "@/api/equip-repair-manage/repair-type";
import equipScrapRecordApi from "@/api/equip-manage/equip-scrap-record";
import userApi from "@/api/system-settings/user-manage";

export default {
  name: "RepairRecord",
  components: {
    Pagination,
    BaseMainSlot,
    AddEditDialog,
    SelectButton,
    ActionBar,
    Video,
    Picture,
    Key,
  },
  mixins: [mymixin],
  data() {
    return {
      // 搜索条件:故障名称：
      hitchTypeCode: "",
      hitchTypeArr: [],
      //  搜索条件:设备名称：
      equipmentCode: "",
      equipmentArr: [],
      //  搜索条件:上报日期
      repairDate: "",
      // 搜索条件:任务名称：
      repairUserId: "",
      userList: [],
      // 委外维修,委内维修
      topitemtypeId: "NATURE_IN",
      topitemSelectList: [
        { id: "NATURE_IN", name: "委内维修", ids: 1 },
        { id: "NATURE_OUT", name: "委外维修", ids: 2 },
      ],
    };
  },
  watch: {},
  async created() {
    this.queryEquipment();
    this.queryhitchTypeList();
    this.queryUserList();
    this.queryPageList();
  },
  mounted() {},
  methods: {
    // 清空选项
    cancelSelect(data) {
      if (data === "hitchTypeCode") {
        this.hitchTypeCode = "";
      }
      if (data === "equipmentCode") {
        this.equipmentCode = "";
      }
      if (data === "repairUserId") {
        this.repairUserId = "";
      }
      this.queryPageList();
    },
    // 选择
    selectHitchTypeCode(id) {
      if (this.hitchTypeCode === id) {
        this.hitchTypeCode = "";
      } else {
        this.hitchTypeCode = id;
      }
      this.queryPageList();
    },
    // 选择
    selectEquipmentCode(id) {
      if (this.equipmentCode === id) {
        this.equipmentCode = "";
      } else {
        this.equipmentCode = id;
      }
      this.queryPageList();
    },
    // 选择
    selectRepairUserId(id) {
      if (this.repairUserId === id) {
        this.repairUserId = "";
      } else {
        this.repairUserId = id;
      }
      this.queryPageList();
    },
    // 条件查询未报废设备
    queryEquipment() {
      this.equipmentArr = [];
      let params = {};
      equipScrapRecordApi.queryEquipment(params).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          for (var i in data) {
            this.equipmentArr.push({
              equipmentName: data[i],
              equipmentCode: i,
            });
          }
        }
      });
    },
    // 条件查询故障类型
    queryhitchTypeList() {
      this.hitchTypeArr = [];
      repairTypeApi.findAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.hitchTypeArr = data;
        }
      });
    },
    // 查询可选择维修人
    queryUserList() {
      userApi.queryUserAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.userList = data;
        }
      });
    },
    getName(v) {
      var index = this.userList.findIndex((item) => item.id === v);
      if (index !== -1) {
        return this.userList[index]["username"];
      }
    },
    // 委内维修,委外维修
    topitemtype(e) {
      this.topitemtypeId = e;
      // 获取表格数据
      this.queryPageList();
    },
    queryPageList() {
      this.loading = true;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          hitchTypeCode: this.hitchTypeCode,
          equipmentCode: this.equipmentCode,
          repairUserId: this.repairUserId,
          repairDate: this.repairDate,
          repairNature: this.topitemtypeId,
        },
      };
      repairRecordApi.queryPageList(param).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },

    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map((item) => {
        if (item.repairTaskCode) {
          return item.repairTaskCode;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          var form = new FormData();
          form.append("repairTaskCodeList", data.repairTaskCode);
          if (this.topitemtypeId === "NATURE_OUT") {
            repairRecordApi.delOutList(form).then((res) => {
              if (res.code === 1) {
                this.$message.success(res.msg);
                this.queryPageList();
                this.resetActionBar();
              }
            });
          }
          if (this.topitemtypeId === "NATURE_IN") {
            repairRecordApi.delInList(form).then((res) => {
              if (res.code === 1) {
                this.$message.success(res.msg);
                this.queryPageList();
                this.resetActionBar();
              }
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 表格删除
    delmessage() {
      var form = new FormData();
      this.checkedIdList.forEach((a) => {
        form.append("repairTaskCodeList", a);
      });
      if (this.topitemtypeId === "NATURE_OUT") {
        repairRecordApi.delOutList(form).then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.queryPageList();
            this.resetActionBar();
          }
        });
      }
      if (this.topitemtypeId === "NATURE_IN") {
        repairRecordApi.delInList(form).then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.queryPageList();
            this.resetActionBar();
          }
        });
      }
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.repairTaskCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
    // 查看详情
    queryDetail(row) {
      this.editObject = JSON.parse(JSON.stringify(row));
      this.nowChanceStau = "showdetail";
      this.titleText = "查看详情";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 查看关键字
    queryKey(row) {
      this.titleText = "关键字";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showKeyDialog = true;
    },
    // 查看视频
    queryVideo(row) {
      this.titleText = "故障视频";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showVideoDialog = true;
    },
    // 查看图片
    queryPicture(row) {
      this.titleText = "故障图片";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showPictureDialog = true;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table {
  .el-icon {
    color: #fff;
  }
  .el-table__expanded-cell {
    background: #4affda00;
  }
}
</style>
